package com.mdd.cms.service.security.impl;

import com.mdd.cms.entity.SecurityStudio;
import com.mdd.cms.mapper.SecurityStudioMapper;
import com.mdd.cms.params.SecurityStudioParams;
import com.mdd.cms.vo.SecurityStudioDetailVo;
import com.mdd.cms.vo.SecurityStudioListVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdd.cms.service.security.SecurityStudioService;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.util.UrlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 安全学习视频操作类
 *
 * @Description
 * @Author 幻速
 * @Date 2024/1/30 10:33
 **/
@Service
public class SecurityStudioServiceImpl extends ServiceImpl<SecurityStudioMapper, SecurityStudio>implements SecurityStudioService {
    @Override
    public PageResult<SecurityStudioListVo> pages(PageValidate pageValidate, String keyWorld) {
        Integer pageNo   = Objects.nonNull(pageValidate)?pageValidate.getPageNo():1;
        Integer pageSize = Objects.nonNull(pageValidate)?pageValidate.getPageSize():10;

        LambdaQueryWrapper<SecurityStudio> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(keyWorld)){
            queryWrapper.like(SecurityStudio::getTitle,keyWorld);
        }
        queryWrapper.orderByDesc(SecurityStudio::getCreateTime);
        IPage<SecurityStudio> iPage = this.baseMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
        List<SecurityStudioListVo> list = new LinkedList<>();
        for(SecurityStudio item : iPage.getRecords()) {
            SecurityStudioListVo vo = new SecurityStudioListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setImgUrl(UrlUtils.toAbsoluteUrl(vo.getImgUrl()));
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public List<SecurityStudioListVo> tops() {
        LambdaQueryWrapper<SecurityStudio> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SecurityStudio::getCreateTime);
        queryWrapper.eq(SecurityStudio::getIsTop,true).last("limit 1");
        List<SecurityStudio> securityStudios = this.baseMapper.selectList(queryWrapper);
        List<SecurityStudioListVo> list = new LinkedList<>();
        for(SecurityStudio item : securityStudios) {
            SecurityStudioListVo vo = new SecurityStudioListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setImgUrl(UrlUtils.toAbsoluteUrl(vo.getImgUrl()));
            list.add(vo);
        }
        return list;
    }

    @Override
    public SecurityStudioDetailVo getDetail(Long sid) {
        LambdaQueryWrapper<SecurityStudio> studioLambdaQW=new LambdaQueryWrapper<>();
        studioLambdaQW.eq(SecurityStudio::getId,sid);
        Assert.isTrue(this.baseMapper.exists(studioLambdaQW),"该内容不存在");
        SecurityStudio securityStudio = this.baseMapper.selectById(sid);
        SecurityStudioDetailVo vo = new SecurityStudioDetailVo();
        BeanUtils.copyProperties(securityStudio, vo);
        vo.setImgUrl(UrlUtils.toAbsoluteUrl(vo.getImgUrl()));
        vo.setVideoUrl(UrlUtils.toAbsoluteUrl(vo.getVideoUrl()));
        return vo;
    }

    @Override
    public Boolean add(SecurityStudioParams securityStudioParams) {
        SecurityStudio securityStudio=new SecurityStudio();
        securityStudioParams.setImgUrl(UrlUtils.toRelativeUrl(securityStudioParams.getImgUrl()));
        securityStudioParams.setVideoUrl(UrlUtils.toRelativeUrl(securityStudioParams.getVideoUrl()));
        BeanUtils.copyProperties(securityStudioParams, securityStudio);
        return save(securityStudio);
    }

    @Override
    public Boolean update(SecurityStudioParams securityStudioParams) {
        SecurityStudio securityStudio = this.baseMapper.selectById(securityStudioParams.getId());
        Assert.notNull(securityStudio,"该安全学习内容不存在");
        if (StringUtils.isNotBlank(securityStudioParams.getTitle())){
            securityStudio.setTitle(securityStudioParams.getTitle());
        }
        if (StringUtils.isNotBlank(securityStudioParams.getImgUrl())){
            securityStudio.setImgUrl(UrlUtils.toRelativeUrl(securityStudioParams.getImgUrl()));
        }
        if (StringUtils.isNotBlank(securityStudioParams.getVideoUrl())){
            securityStudio.setVideoUrl(UrlUtils.toRelativeUrl(securityStudioParams.getVideoUrl()));
        }
        if (Objects.nonNull(securityStudioParams.getIsTop())){
            securityStudio.setIsTop(securityStudioParams.getIsTop());
        }
        return saveOrUpdate(securityStudio);
    }
    @Override
    public Boolean del(Long sid) {
        Assert.notNull(sid,"请传入要删除的内容ID");
        return this.baseMapper.deleteById(sid) > 0;
    }
}
